//
//  AddLadingBillVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/7/2.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "AddLadingBillVC.h"
#import "AddLadingBillStatusView.h"
#import "AddLadingBillModel.h"
#import "LadingRequireMainModel.h"
#import "AddLadingBillCell.h"
#import "ChooseHeTongVC.h"
#import "ChooseAddressVC.h"
#import "BRDatePickerView.h"
#import "AddLadingBillTwoVC.h"
#import "SupplierChooseVC.h"
#import "LJDatePickerView.h"

@interface AddLadingBillVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) AddLadingBillStatusView *statusView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) AddLadingBillModel *addModel;

@end

@implementation AddLadingBillVC

-(AddLadingBillStatusView *)statusView {
    if (!_statusView) {
        _statusView = [[AddLadingBillStatusView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 75)];
        _statusView.status = 1;
    }
    return _statusView;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, _bottomView.nim_width - 60, 44)];
        [nextBtn setBackgroundColor:Color_ThemeColor];
        nextBtn.layer.masksToBounds = YES;
        nextBtn.layer.cornerRadius = 6;
        [nextBtn setTitle:@"下一步" forState:0];
        [nextBtn setTitleColor:HexColor(0xffffff) forState:0];
        nextBtn.titleLabel.font = BoldFontOfSize(16);
        [nextBtn addTarget:self action:@selector(nextClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:nextBtn];
    }
    return _bottomView;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.statusView.nim_bottom, ScreenW, ScreenH - self.bottomView.nim_height - NaviHeight - self.statusView.nim_height)];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

-(AddLadingBillModel *)addModel {
    if (!_addModel) {
        _addModel = [AddLadingBillModel new];
    }
    return _addModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新建提货单";
    [self.view addSubview:self.statusView];
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.tableView];
    [self requestForEdit];
}

- (void)requestForEdit {
    if (self.dp_id.length) {
        [HttpRequest GetWithApi:ReceiptDeliveryGetLadingBillInfo params:@{@"dp_id":self.dp_id} progress:^(NSProgress * _Nonnull progress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
            if (code) {
                //合同
                HeTongMainModel *heTong = [HeTongMainModel new];
                heTong.co_id = [responseObject objectForKey:@"co_id"];
                heTong.constract_sn = [responseObject objectForKey:@"constract_sn"];
                heTong.name = [responseObject objectForKey:@"name"];
                heTong.type = [responseObject objectForKey:@"settleStyName"];
                self.addModel.heTong = heTong;
                //提货地址
                self.addModel.tiAddress = [AddressMainModel mj_objectWithKeyValues:[responseObject objectForKey:@"pickup_address"]];
                self.addModel.tiAddress.w_id = [responseObject objectForKey:@"w_id"];
                //收货地址
                self.addModel.shouAddress = [AddressMainModel mj_objectWithKeyValues:[responseObject objectForKey:@"collect_address"]];
                self.addModel.shouAddress.sa_id = [responseObject objectForKey:@"sa_id"];
                //供应商
                SupplierMainModel *supplier = [SupplierMainModel new];
                supplier.t_id = [responseObject objectForKey:@"t_id"];
                supplier.name = [responseObject objectForKey:@"name"];
                supplier.id_allow = [[responseObject objectForKey:@"id_allow"] intValue];
                self.addModel.supplier = supplier;
                
                self.addModel.aorder = [responseObject objectForKey:@"aorder"];
                self.addModel.ynum = [responseObject objectForKey:@"ynum"];
                self.addModel.beginTime = [responseObject objectForKey:@"startTime"];
                self.addModel.endTime = [responseObject objectForKey:@"endTime"];
                self.addModel.requireDate = [responseObject objectForKey:@"arrivalTime"];
                
                LadingRequireMainModel *requireModel = [LadingRequireMainModel new];
                requireModel.deliveryId = [[responseObject objectForKey:@"delivery"] intValue];
                requireModel.tsTimeId = [[responseObject objectForKey:@"tsTime"] intValue];
                requireModel.shippingModeId = [responseObject objectForKey:@"transport"];
                requireModel.carRequireModeId = [responseObject objectForKey:@"needCar"];
                requireModel.allWeight = [responseObject objectForKey:@"allWeight"];
                requireModel.allVolume = [responseObject objectForKey:@"allVolume"];
                requireModel.jieSuanTypeId = [responseObject objectForKey:@"settleSty"];
                requireModel.insuranceValue = [responseObject objectForKey:@"price"];
                requireModel.files = [responseObject objectForKey:@"fileInfo"];
                requireModel.remark = [responseObject objectForKey:@"remarks"];
                self.addModel.requireModel = requireModel;
                
                [self.tableView reloadData];
            }
        } fail:^(NSError * _Nonnull error) {
            
        }];
    }
}

- (void)nextClick {
    if (!self.addModel.supplier.t_id.length) {
        [MBProgressHUD leShowFailedHUD:MAIN_WINDOW message:@"请选择供应商"];
        return;
    }
    if (self.addModel.supplier.id_allow == 1) {
        if (!self.addModel.heTong) {
            [MBProgressHUD leShowFailedHUD:MAIN_WINDOW message:@"请选择合同"];
            return;
        }
    }
    if (!self.addModel.tiAddress) {
        [MBProgressHUD leShowFailedHUD:MAIN_WINDOW message:@"请选择提货地址"];
        return;
    }
    if (!self.addModel.shouAddress) {
        [MBProgressHUD leShowFailedHUD:MAIN_WINDOW message:@"请选择收货地址"];
        return;
    }
    if (!self.addModel.beginTime.length) {
        [MBProgressHUD leShowFailedHUD:MAIN_WINDOW message:@"请设置开始提货时间"];
        return;
    }
    if (!self.addModel.endTime.length) {
        [MBProgressHUD leShowFailedHUD:MAIN_WINDOW message:@"请设置截止提货时间"];
        return;
    }
    if (!self.addModel.requireDate.length) {
        [MBProgressHUD leShowFailedHUD:MAIN_WINDOW message:@"请设置要求到达时间"];
        return;
    }
    AddLadingBillTwoVC *vc = [AddLadingBillTwoVC new];
    vc.billModel = self.addModel;
    if (self.dp_id.length) {
        vc.dp_id = self.dp_id;
    }
    [self.rt_navigationController pushViewController:vc animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddLadingBillCell *cell = [AddLadingBillCell AddLadingBillCellWithTableView:tableView];
    cell.model = self.addModel;
    __weak_self;
    cell.supplierBlock = ^{
        //选择供应商
        if (!self.dp_id.length) {
            SupplierChooseVC *vc = [SupplierChooseVC new];
            __weak_self;
            vc.finishBlock = ^(SupplierMainModel * _Nonnull model) {
                weakself.addModel.supplier = model;
                [tableView reloadData];
            };
            [weakself.rt_navigationController pushViewController:vc animated:YES];
        }
    };
    cell.heTongBlock = ^{
        //选择合同
        if (weakself.addModel.supplier.t_id.length) {
            ChooseHeTongVC *vc = [ChooseHeTongVC new];
            vc.t_id = weakself.addModel.supplier.t_id;
            vc.finishBlock = ^(HeTongMainModel * _Nonnull heTongModel) {
                weakself.addModel.heTong = heTongModel;
                [tableView reloadData];
            };
            [weakself.rt_navigationController pushViewController:vc animated:YES];
        }else {
            [MBProgressHUD leShowFailedHUD:weakself.view message:@"请先选择供应商"];
        }
    };
    cell.tiAddressBlock = ^{
        //选择提货地址
        if (weakself.addModel.supplier.t_id.length) {
            ChooseAddressVC *vc = [ChooseAddressVC new];
            vc.isTiHuo = YES;
            vc.t_id = weakself.addModel.supplier.t_id;
            vc.finishBlock = ^(AddressMainModel * _Nonnull addressModel) {
                weakself.addModel.tiAddress = addressModel;
                [tableView reloadData];
            };
            [weakself.rt_navigationController pushViewController:vc animated:YES];
        }else {
            [MBProgressHUD leShowFailedHUD:weakself.view message:@"请先选择供应商"];
        }
    };
    cell.shouAddressBlock = ^{
        //选择收货地址
        if (weakself.addModel.supplier.t_id.length) {
            ChooseAddressVC *vc = [ChooseAddressVC new];
            vc.isTiHuo = NO;
            vc.t_id = weakself.addModel.supplier.t_id;
            vc.finishBlock = ^(AddressMainModel * _Nonnull addressModel) {
                weakself.addModel.shouAddress = addressModel;
                [tableView reloadData];
            };
            [weakself.rt_navigationController pushViewController:vc animated:YES];
        }else {
            [MBProgressHUD leShowFailedHUD:weakself.view message:@"请先选择供应商"];
        }
    };
    cell.beginBlock = ^{
        [BRDatePickerView showDatePickerWithTitle:@"开始时间" dateType:UIDatePickerModeDateAndTime defaultSelValue:nil minDateStr:[TimeModel stringFromDate:[NSDate date] andFormatter:@"yyyy/MM/dd HH:mm:ss"] maxDateStr:nil isAutoSelect:YES resultBlock:^(NSString *selectValue) {
            weakself.addModel.beginTime = selectValue;
            [tableView reloadData];
        }];
//        LJDatePickerView *picker = [[LJDatePickerView alloc] initWithDateMode:LJDatePickerViewModeDateMinSec];
//        picker.currentDate = weakself.addModel.beginTime.length ? weakself.addModel.beginTime : [TimeModel stringFromDate:[NSDate date] andFormatter:@"yyyy/MM/dd HH:mm"];
//        [picker showWithDate:^(NSString * _Nonnull dateStr) {
//            weakself.addModel.beginTime = dateStr;
//            [tableView reloadData];
//        }];
    };
    cell.endBlock = ^{
        if (!weakself.addModel.beginTime.length) {
            [MBProgressHUD leShowFailedHUD:MAIN_WINDOW message:@"请先选择开始时间"];
            return;
        }
        [BRDatePickerView showDatePickerWithTitle:@"结束时间" dateType:UIDatePickerModeDateAndTime defaultSelValue:nil minDateStr:[TimeModel getScheduledTimeWithTimeStr:weakself.addModel.beginTime inFormatter:@"yyyy/MM/dd HH:mm" outFormatter:@"yyyy/MM/dd HH:mm:ss"] maxDateStr:nil isAutoSelect:YES resultBlock:^(NSString *selectValue) {
            weakself.addModel.endTime = selectValue;
            [tableView reloadData];
        }];
//        LJDatePickerView *picker = [[LJDatePickerView alloc] initWithDateMode:LJDatePickerViewModeDateMinSec];
//        picker.currentDate = weakself.addModel.endTime.length ? weakself.addModel.endTime : [TimeModel stringFromDate:[NSDate date] andFormatter:@"yyyy/MM/dd HH:mm"];
//        [picker showWithDate:^(NSString * _Nonnull dateStr) {
//            weakself.addModel.endTime = dateStr;
//            [tableView reloadData];
//        }];
    };
    cell.dateBlock = ^{
//        LJDatePickerView *picker = [[LJDatePickerView alloc] initWithDateMode:LJDatePickerViewModeDateMinSec];
//        picker.currentDate = weakself.addModel.requireDate.length ? weakself.addModel.requireDate : [TimeModel stringFromDate:[NSDate date] andFormatter:@"yyyy/MM/dd HH:mm"];
//        [picker showWithDate:^(NSString * _Nonnull dateStr) {
//            weakself.addModel.requireDate = dateStr;
//            [tableView reloadData];
//        }];
        if (!weakself.addModel.endTime.length) {
            [MBProgressHUD leShowFailedHUD:MAIN_WINDOW message:@"请先选择截止时间"];
            return;
        }
        [BRDatePickerView showDatePickerWithTitle:@"到达时间" dateType:UIDatePickerModeDateAndTime defaultSelValue:nil minDateStr:[TimeModel getScheduledTimeWithTimeStr:weakself.addModel.endTime inFormatter:@"yyyy/MM/dd HH:mm" outFormatter:@"yyyy/MM/dd HH:mm:ss"] maxDateStr:nil isAutoSelect:YES resultBlock:^(NSString *selectValue) {
            weakself.addModel.requireDate = selectValue;
            [tableView reloadData];
        }];
    };
    cell.linkOrderBlock = ^(NSString * _Nonnull str) {
        weakself.addModel.aorder = str;
    };
    cell.oriOrderBlock = ^(NSString * _Nonnull str) {
        weakself.addModel.ynum = str;
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddLadingBillCell *cell = [AddLadingBillCell AddLadingBillCellWithTableView:tableView];
    cell.model = self.addModel;
    return cell.height;
}

@end
